package com.hmyd.hmyd_android.shop;

import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;

import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.fragment.BasicFragment;
import com.hmyd.hmyd_android.view.LPWebView;
import com.hmyd.hmyd_android.view.LPWebView.OnScrollChangeListener;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

/**
 * 商品规格参数
 * 
 * @author wangc
 * 
 */
public class ParametersFragment extends BasicFragment {

	private View rootView;
	private final int REFRESH = 1;
	private LPWebView mWb;
	private WebSettings webSettings;

	public ParametersFragment(){}

	@Override
	public View onCreateView(LayoutInflater inflater,  ViewGroup container,
			 Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_goods_parameters, null);
		return rootView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		// 绑定控件
		mWb = (LPWebView) rootView.findViewById(R.id.webview);

		webSettings = mWb.getSettings();

		// 设置数据
		webSettings.setJavaScriptEnabled(true); // 支持JS
	}

	@Override
	public void onResume() {
		super.onResume();
		RequestParams params = new RequestParams();
		params.addBodyParameter("itemId", getArguments().getString("itemId"));
		onRequest(REFRESH, UrlConstant.PARAMETERS, HttpMethod.GET, params);
	}

	@Override
	public void onPostSuccess(int postId, String result) {
		if (TextUtils.isEmpty(result)) {
			return;
		}
		String res = null;
		try {
			JSONObject jsonObject = new JSONObject(result);
			res = jsonObject.getString("data");
			JSONObject jsonObject2 = new JSONObject(res);
			String reString = jsonObject2.getString("specification");
			// 除去数据中的字体大小设置
			if (!TextUtils.isEmpty(reString) && reString.contains("font-size:")) {
				StringBuilder builder = new StringBuilder();
				String[] args = reString.split("font-size:");
				for (int i = 0; i < args.length; i++) {
					if (i == 0) {
						builder.append(args[i]);
					} else {
						int index = args[i].indexOf("\"");
						builder.append(args[i].substring(index));
					}
				}
				
				reString = builder.toString();
			}
			// 将html中所有td替换为tr，暂时避免table错版问题
			if (!TextUtils.isEmpty(reString) && reString.contains("td")) {
				StringBuilder builder = new StringBuilder();
				String[] args = reString.split("td");
				for (int i = 0; i < args.length; i++) {
					if(i<args.length-1) {
						builder.append(args[i]+"tr");
					} else {
						builder.append(args[i]);
					}
				}
				reString = builder.toString();
			}
			
			if (!TextUtils.isEmpty(reString) && reString.contains("</tr>")) {
				StringBuilder builder = new StringBuilder();
				String[] args = reString.split("</tr>");
				for (int i = 0; i < args.length; i++) {
					if(i<args.length-1) {
						builder.append(args[i]+"</tr>&nbsp;&nbsp");
					} else {
						builder.append(args[i]);
					}
				}
				reString = builder.toString();
			}
			
			if (!TextUtils.isEmpty(reString) && reString.contains("</tr>")) {
				StringBuilder builder = new StringBuilder();
				String[] args = reString.split("</tr>");
				for (int i = 0; i < args.length; i++) {
					if(i<args.length-1) {
						builder.append(args[i]+"</tr>&nbsp;&nbsp");
					} else {
						builder.append(args[i]);
					}
				}
				reString = builder.toString();
			}
			if (!TextUtils.isEmpty(reString) && reString.contains("line-height:")) {
				StringBuilder builder = new StringBuilder();
				String[] args = reString.split("line-height:");
				for (int i = 0; i < args.length; i++) {
					if(i<args.length-1) {
						builder.append(args[i]);
					} else {
						builder.append(args[i]);
					}
				}
				reString = builder.toString();
			}
			
//			int fontSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16,
//					getResources().getDisplayMetrics());
			webSettings.setDefaultFontSize(36);
			webSettings.setUseWideViewPort(true);
			webSettings.setLoadWithOverviewMode(true);
			webSettings.setDefaultTextEncodingName("UTF-8");
//			mWb.loadData(temp, "text/html;charset=UTF-8", null);
			mWb.loadDataWithBaseURL(null, reString, "text/html", HTTP.UTF_8, null);

			mWb.setOnScrollChangedListener(listener);
		} catch (JSONException e) {
			e.printStackTrace();
		}

	}

	/**
	 * webView滚动监听
	 */
	OnScrollChangeListener listener = new OnScrollChangeListener() {

		@Override
		public void onScrollChanged(int l, int t, int oldl, int oldt) {
		}

		@Override
		public void onPageTop(int l, int t, int oldl, int oldt) {
			Intent intent = new Intent("isTop");
			intent.putExtra("isTop", "isTop");
			getActivity().sendBroadcast(intent);
		}

		@Override
		public void onPageEnd(int l, int t, int oldl, int oldt) {
			Intent intent = new Intent("isTop");
			intent.putExtra("isTop", "notTop");
			getActivity().sendBroadcast(intent);
		}
	};

	@Override
	public void onPostFailure(int postId, String msg) {
	}
}
